package com.cloud.pole.shop.biz.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.cloud.pole.common.biz.BaseBiz;
import com.cloud.pole.mapper.goods.ShopGoodsPromotionMapper;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsPromotion;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsPromotionAdminVo;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsPromotionVo;

import tk.mybatis.mapper.entity.Example;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-08 16:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShopGoodsPromotionBiz extends BaseBiz<ShopGoodsPromotionMapper, ShopGoodsPromotion> {
	
	@Cache(key="shopGoodsPromotion:promotionType{1}", expire=5)
    public List<ShopGoodsPromotion> selectListByPromotionType(String promotionType) {
		 Example example = new Example(ShopGoodsPromotion.class);
		 example.setOrderByClause("disp_order");
 	     example.createCriteria().andEqualTo("promotionType", promotionType);
		 return selectByExample(example);
    }
	
	@Cache(key="shopGoodsPromotionVo:promotionType{1}", expire=5)
	public List<ShopGoodsPromotionVo> selectShopGoodsPromotionVoListByMap(String promotionType) {
		 Map<String, Object> map = new HashMap<String, Object>();
		 map.put("promotionType", promotionType);
		 return mapper.selectShopGoodsPromotionVoListByMap(map);
    }
	
	@Cache(key="shopGoodsPromotionVo{1}-shopId-promotion{2}-promotionType", expire=5)
	public List<ShopGoodsPromotionVo> selectShopGoodsPromotionVoListByMap(Integer shopId, String promotionType) {
		 Map<String, Object> map = new HashMap<String, Object>();
		 map.put("promotionType", promotionType);
		 map.put("shopId", shopId);
		 return mapper.selectShopGoodsPromotionVoListByMap(map);
    }
	
	@Cache(key="shopGoodsPromotionVo-componentId{1}", expire=1)
	public List<ShopGoodsPromotionVo> selectShopGoodsPromotionVoListByMap(Integer componentId) {
		 Map<String, Object> map = new HashMap<String, Object>();
		 map.put("componentId", componentId);
		 return mapper.selectShopGoodsPromotionVoListByMap(map);
    }
	
	/**
	 * 根据产品id查询是否有推荐
	 * @param goodsId
	 * @return
	 */
	public ShopGoodsPromotion selectShopGoodsPromotionByGoodsId(Integer goodsId) {
		ShopGoodsPromotion entity = new ShopGoodsPromotion();
		entity.setGoodsId(goodsId);
		return super.selectOne(entity);
	}
	
	/**
	 * 清除缓存，删除记录（删除前调用selectShopGoodsPromotionByGoodsId查询是否有记录）
	 * @param entity
	 */
	@CacheClear(keys= {"shopGoodsPromotion:promotionType{1.promotionType}", 
						"shopGoodsPromotionVo:promotionType{1.promotionType}",
						"shopGoodsPromotionVo{1.shopId}-shopId-promotion{1.promotionType}-promotionType"})
	public void deleteShopGoodsPromotion(ShopGoodsPromotion entity) {
	     super.deleteById(entity.getPromotionId());
	}
	
	/**
	 * 清除缓存，删除记录（删除前调用selectShopGoodsPromotionByGoodsId查询是否有记录）
	 * @param entity
	 */
	@CacheClear(keys= {"shopGoodsPromotion:promotionType{1.promotionType}", 
				       "shopGoodsPromotionVo:promotionType{1.promotionType}",
						"shopGoodsPromotionVo{1.shopId}-shopId-promotion{1.promotionType}-promotionType"})
	public void updateShopGoodsPromotion(ShopGoodsPromotion entity) {
	     super.updateById(entity);
	}
	
	@Override
	/**
	 * 清除缓存，删除记录（删除前调用selectShopGoodsPromotionByGoodsId查询是否有记录）
	 * @param entity
	 */
	@CacheClear(keys= {"shopGoodsPromotion:promotionType{1.promotionType}", 
				       "shopGoodsPromotionVo:promotionType{1.promotionType}",
						"shopGoodsPromotionVo{1.shopId}-shopId-promotion{1.promotionType}-promotionType"})
	public void insertSelective(ShopGoodsPromotion entity) {
		super.insertSelective(entity);
	}
	
	/**
	 * 后台接口
	 * @param map
	 * @return
	 */
	public List<ShopGoodsPromotionAdminVo> selectShopGoodsPromotionAdminVoListByMap(Map<String, Object> map){
		return mapper.selectShopGoodsPromotionAdminVoListByMap(map);
	}
	
	public void addShopGoodsPromotionList(List<ShopGoodsPromotion> shopGoodsPromotionList) {
		mapper.addShopGoodsPromotionListMa(shopGoodsPromotionList);
	}
	
	public List<ShopGoodsPromotion> selectPageShopAdminList(Integer componentId,Integer shopId){
		return mapper.selectAllPage(componentId, shopId);
	}
	
	public ShopGoodsPromotion selectById(Integer shopId, Integer indexRecommNum, Integer promotionId) {
		return mapper.selectByIdMa(shopId,indexRecommNum,promotionId);
	}
	
    
	public void updateOpenShopGoodsPromotion(ShopGoodsPromotion shopGoodsPromotion) {
		mapper.updateShopGoodsPromotionMa(shopGoodsPromotion);
	}
}
